import React, { useEffect, useRef, useState } from 'react';

function ExampleUseRef() {
    const inputEl = useRef(null)
    const onButton = ()=>{
        console.log(inputEl)
        inputEl.current.value ='hello'
        console.log(inputEl)
    }
    const [text,setText] = useState('小鬼')
    //通过ref保存变量
    const textRef = useRef()
    useEffect(()=>{
        textRef.current = text
        console.log('textRef.current' , textRef.current)
    })
    return( 
        <>
            <input ref={inputEl} type='text'/>
            <button onClick={()=>{onButton()}}>在input上展现文字</button>
            <br />
            <br />
            <input value={text} onChange={(e)=>{setText(e.target.value)}}/>
        </>
    )
}

export default ExampleUseRef;